/*----------------------------------------------
| noka star v1.0 www.97521.com                 |
| rebin 2017-04-20                             |
|---------------------------------------------*/
var nstar = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.id = cfg.id;				//id
    	this.maxvalue = (undefined==cfg.maxvalue?5:cfg.maxvalue);	//最大值
    	this.value = (undefined==cfg.value?0:cfg.value);			//值
    	this.name = cfg.name;			//名字
    	this.onclick = cfg.onclick; 	//单击
    	this.readonly = cfg.readonly;
    	this.disabled = cfg.disabled;
    	this.onchange = cfg.onchange;
    	this.cfg = cfg;
    },
    show : function(){
    	var self = this;
    	self.initStar();
    	self.RowEvent();
    	self.starevent();
    },
    //-------------------初始化-----------------------------
    initStar : function(){
    	var self = this;
    	var idx = 0;
    	var html = [];
    	if(self.maxvalue<self.value){
    		self.maxvalue=self.value;
    	}
    	html[idx++]='<input type="hidden" id="'+self.id+'" name="'+(undefined==self.name?'':self.name)+'" value="'+(undefined==self.value?'':self.value)+'"/>';
    	for(var i=1;i<=self.maxvalue;i++){
    		if(self.value>=i){
    			html[idx++]='<div id="nk_'+self.id+'_star_row_'+i+'" class="noka_star noka_star_on">&nbsp;</div>';
			}else{
				html[idx++]='<div id="nk_'+self.id+'_star_row_'+i+'" class="noka_star noka_star_off">&nbsp;</div>';
			}
    	}
    	$(self.id+'_div').innerHTML = html.join('');
    },
    //----------------选项事件-----------------------------
    RowEvent : function(){
    	var self = this;
    	$$('div[id^="nk_'+self.id+'_star_row_"]').each(function(alink,index) {
   		 //------------单击事件----------------------------------------------------
   		 $(alink).observe('click', function() {
   			if('readonly'==self.readonly || 'disabled'==self.disabled){//如果不只读
   				return;
   			}
	        self.value = parseInt(alink.id.substring(alink.id.lastIndexOf('_')+1,alink.id.length));
	        if($(self.id).value == self.value && self.value==1){//
	            $(self.id).value = self.value = 0;
	        }else{
	            $(self.id).value = self.value;
	        }
	        if(undefined != self.onclick){
	        	try{ self.onclick($(self.id)); }catch(e){}
	        }
	        if(undefined != self.onchange){
	        	try{ self.onchange($(self.id)); }catch(e){}
	        }
	        self.setIdxStyle(self.value);
	     });
   		 //---------------鼠标移入------------------------------------------------
   		 $(alink).observe('mousemove', function() {
   			if('readonly'==self.readonly || 'disabled'==self.disabled){//如果不只读
   				return;
   			}
   			var idx = parseInt(alink.id.substring(alink.id.lastIndexOf('_')+1,alink.id.length));
	            self.setIdxStyle(idx);
         });
   		 //--------------鼠标移出-------------------------------------------------
         $(alink).observe('mouseout', function() {
            if('readonly'==self.readonly || 'disabled'==self.disabled){//如果不只读
       			return;
       		}
           	self.setIdxStyle(self.value);
          });
		});
    },
    //---------------外部事件---------------------------------------------------------
    starevent : function(){
    	var self = this;
    	$(self.id).veri = function(){
    		return true;
    	};
    	//---------设置值-----------------------------------------------------------
    	$(self.id).setValue = function(value,ch){
    		$(self.id).value = value;
    		self.value = value;
    		self.setIdxStyle(self.value);
    	};
    	//----------设置最大值-------------------------------------------------------
    	$(self.id).setMaxValue = function(value){
    		if(value>=self.value){
    			self.maxvalue = value;
    			self.initStar();
    			self.RowEvent();
    		}
    	};
    	//---------获取值--------------------------------------------------------
    	$(self.id).getValue = function(){
    		return $(self.id).value;
    	};
    	//---------设置只读-----------------------------------------------------
    	$(self.id).setReadonly = function(a){
    		self.readonly = (a?'readonly':'');
    	};
    	$(self.id).setDisabled = function(a){
    		self.disabled = (a?'disabled':'');
    	};
    	//-----------动态表单--------------------------------------------------
    	document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
   		if(self.id.indexOf('_nkADinsert_')==-1){
	    		var npfffs=event.memo.widgetNumber.clone();//[{}]
	    		for(var i=0;i<npfffs.length;i++){
	    			var cfgid = Object.allclone(self.cfg);
	    			var initem = npfffs[i];
	    			cfgid.id = initem.id;
	    			if(cfgid.id.indexOf('_'+self.id+'_')!=-1){
	    				(new nstar(cfgid)).show();
	    				try{
	    					var idx = cfgid.id.substring(cfgid.id.lastIndexOf('_')+1,cfgid.id.length);
	    					$(cfgid.id).name=$(cfgid.id).name.replace(/\[0\]/g,'['+idx+']');
	    					if(undefined!=initem.value && $(cfgid.id).name==initem.name){
	    						$(cfgid.id).setValue(initem.value);
	    					}
	    				}catch(e){}
	    			}
	    		}
   		 }
   	 });
    },
    //----------------星标高亮---------------------------------------------
    setIdxStyle : function(idx){
    	var self = this;
    	$$('div[id^="nk_'+self.id+'_star_row_"]').each(function(alink,index) {
    		alink.removeClassName('noka_star_on');
    		alink.addClassName('noka_star_off');
    	});
    	for(var i=1;i<=idx;i++){
    		var rowobj = $('nk_'+self.id+'_star_row_'+i);
    		rowobj.removeClassName('noka_star_off');
    		rowobj.addClassName('noka_star_on');
    	}
    }
});